package cn.iocoder.yudao.module.wengfu.controller.admin.base;

import cn.iocoder.yudao.module.wengfu.framework.excel.core.util.ExcelUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

import cn.iocoder.yudao.module.wengfu.controller.admin.base.vo.*;
import cn.iocoder.yudao.module.wengfu.dal.dataobject.base.SampleBaseInfoDO;
import cn.iocoder.yudao.module.wengfu.service.base.SampleBaseInfoService;

@Tag(name = "管理后台 - 样品基础信息")
@RestController
@RequestMapping("/wengfu/sample-base-info")
@Validated
public class SampleBaseInfoController {

    @Resource
    private SampleBaseInfoService sampleBaseInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建样品基础信息")
    @PreAuthorize("@ss.hasPermission('wengfu:sample-base-info:create')")
    public CommonResult<String> createSampleBaseInfo(@Valid @RequestBody SampleBaseInfoSaveReqVO createReqVO) {
        return success(sampleBaseInfoService.createSampleBaseInfo(getLoginUserId(),createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新样品基础信息")
    @PreAuthorize("@ss.hasPermission('wengfu:sample-base-info:update')")
    public CommonResult<Boolean> updateSampleBaseInfo(@Valid @RequestBody SampleBaseInfoSaveReqVO updateReqVO) {
        sampleBaseInfoService.updateSampleBaseInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除样品基础信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('wengfu:sample-base-info:delete')")
    public CommonResult<Boolean> deleteSampleBaseInfo(@RequestParam("id") Long id) {
        sampleBaseInfoService.deleteSampleBaseInfo(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除样品基础信息")
                @PreAuthorize("@ss.hasPermission('wengfu:sample-base-info:delete')")
    public CommonResult<Boolean> deleteSampleBaseInfoList(@RequestParam("ids") List<Long> ids) {
        sampleBaseInfoService.deleteSampleBaseInfoListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得样品基础信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('wengfu:sample-base-info:query')")
    public CommonResult<SampleBaseInfoRespVO> getSampleBaseInfo(@RequestParam("id") Long id) {
        SampleBaseInfoDO sampleBaseInfo = sampleBaseInfoService.getSampleBaseInfo(id);
        return success(BeanUtils.toBean(sampleBaseInfo, SampleBaseInfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得样品基础信息分页")
    @PreAuthorize("@ss.hasPermission('wengfu:sample-base-info:query')")
    public CommonResult<PageResult<SampleBaseInfoRespVO>> getSampleBaseInfoPage(@Valid SampleBaseInfoPageReqVO pageReqVO) {
        PageResult<SampleBaseInfoDO> pageResult = sampleBaseInfoService.getSampleBaseInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SampleBaseInfoRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出样品基础信息 Excel")
    @PreAuthorize("@ss.hasPermission('wengfu:sample-base-info:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportSampleBaseInfoExcel(@Valid SampleBaseInfoPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SampleBaseInfoDO> list = sampleBaseInfoService.getSampleBaseInfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "样品基础信息.xls", "数据", SampleBaseInfoRespVO.class,
                        BeanUtils.toBean(list, SampleBaseInfoRespVO.class));
    }

}
